/*
 *  Encastre.cpp
 *  EPPI-0.0
 *
 *  Created by Juan Carlos Garcia Orden on 19/05/2011
 *
 */

#include "encastre.h"

Encastre::Encastre(ifstream &in, string cname, MecSystem *pSys)
:	Joint(in,cname,pSys),
	endTime(0.0)
//	pointName(),

{	
	dim = parentSystem->dim;	/** Must be set always.  dim belongs to the mother class Body */
	
	char keyword[20];
	string auxs;				/** Aux string */
	vector<string> auxsv(1);	/** Aux string vector */
//	int numPoints;
	int i;
	vector<string>	vBodyNames;
	string line;
	stringstream stnum;
	string fixedofs = "all";
	string sysName;
	
	
	while (in >> keyword && strcmp(keyword,"ENDJOINT") ) 
	{		
		if(!strcmp(keyword,"DOFS"))
		{
			in >> fixedofs;
		}
		if(!strcmp(keyword,"ENDTIME"))
		{
			in >> endTime;
		}				
		if(!strcmp(keyword,"POINTS"))
		{
			getline(in,line);
			//cout << "line is " << line << "\n";
			istringstream iss(line);
			while (iss >> keyword )
			{
				auxs = keyword;
				//cout << "keyword is " << keyword << endl;
				if(checkForSlash(auxs)==false && !(parentSystem->pointNames).count(auxs))	// belongs to this system because does not have slash and it is in the point list
				{
					addSystemName(auxs,parentSystem->name);
					parentSystem->addNoDofsPoint(auxs,fixedofs,endTime);
				}
				else	// belongs to other system
				{
					sysName = getStringHead(auxs);
					parentSystem->getPTSystem(sysName)->addNoDofsPoint(auxs,fixedofs,endTime);
				}
			}
		} else if(!strcmp(keyword,"POINTSET"))
		{
			string psetname;
			in >> psetname;					/** Reads the name of the set of points */
			if(checkForSlash(psetname)==false)	// local pointset
			{
				addSystemName(psetname,parentSystem->name);
				for(i=0; i<parentSystem->pointSets[psetname].size(); ++i)
				{
					auxs = parentSystem->pointSets[psetname][i];
					parentSystem->addNoDofsPoint(auxs,fixedofs,endTime);
				}
			}
			else	// it may belong to other system
			{
				sysName = getStringHead(psetname);
				for(i=0; i<parentSystem->pointSets[psetname].size(); ++i)
				{
					auxs = parentSystem->pointSets[psetname][i];
					parentSystem->getPTSystem(sysName)->addNoDofsPoint(auxs,fixedofs,endTime);
				}
			}
		}
	}
	if(parentSystem->getProcNum()==parentSystem->getRankNum() ) cout << "	Joint " << name << " succesfully read \n";
}

// ***

void Encastre::setPTPointsCons()
{}

// ***

void Encastre::setPPointSurface()
{}

// ***

void Encastre::writeMSH(ofstream &fGraph)
{}

// ***

void Encastre::writeVTU(ofstream &fGraph)
{
	localFGraph << "<?xml version=\"1.0\"?>\n";
	localFGraph << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
	localFGraph << "	<UnstructuredGrid>\n";
	localFGraph << "		<Piece NumberOfPoints=\"" << 0 << "\" NumberOfCells=\"" << 0 << "\">\n";
	localFGraph << "			<Points>\n";
	localFGraph << "				<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "			</Points>\n";
	localFGraph<< "			<Cells>\n";
	localFGraph << "				<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">";
	
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "				<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "				<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "					";

	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph<< "			</Cells>\n";
	
	localFGraph << "		</Piece>\n";
	
	localFGraph << "	</UnstructuredGrid>\n";
	localFGraph << "</VTKFile>\n";
}

// ***

void Encastre::writeOOGL(ofstream &fGraph)
{}